package com.sunny.ynhz.utils;

import android.support.annotation.NonNull;
import android.util.Log;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.sunny.ynhz.bean.LocationBean;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hhh on 2017/12/12.
 */

public class CarUtils {
    public float distance = 0;
    public List<LocationBean> mList = new ArrayList<>();
    static CarUtils carUtils;

    public static CarUtils getInstanceCarutils() {
        if (carUtils == null) {
            carUtils = new CarUtils();
        }
        return carUtils;

    }

    public String getDistance(List<LocationBean> list) {
        if (list.size() < 2) {
            return "0.0m";
        } else if (mList.size() == 0) {
            mList.clear();
            mList.addAll(list);
            for (int i = 1; i < list.size(); i++) {
                LocationBean startLatlng = list.get(i - 1);
                LocationBean endLatlng = list.get(i);
                float mDistance = AMapUtils.calculateLineDistance(new LatLng(startLatlng.getLttd(), startLatlng.getLgtd()), new LatLng(endLatlng.getLttd(), endLatlng.getLgtd()));
                distance = distance + mDistance;
            }
        } else if (mList.size() == list.size()) {
            return toStr(distance);
        } else if (mList.size() != list.size()) {
            mList.clear();
            mList.addAll(list);
            LocationBean startLatlng = list.get(list.size() - 2);
            LocationBean endLatlng = list.get(list.size() - 1);
            float mDistance = AMapUtils.calculateLineDistance(new LatLng(startLatlng.getLttd(), startLatlng.getLgtd()), new LatLng(endLatlng.getLttd(), endLatlng.getLgtd()));
            distance = distance + mDistance;

        }
        return toStr(distance);
    }

    public void resetCurrentDistance() {
        mList.clear();
        distance = 0;
    }

    public static String toStr(float distance) {
        if (distance > 1000) {
            distance = distance / 1000;
            DecimalFormat fnum = new DecimalFormat("##0.00");
            String dd = fnum.format(distance);
            return dd + "km";
        } else {
            DecimalFormat fnum = new DecimalFormat("##0.00");
            String dd = fnum.format(distance);
            return dd + "m";
        }
    }

}
